<?php
header("Content-Type: application/json; charset=UTF-8");
require_once '../db.php';

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$pageSize = isset($_GET['pageSize']) ? (int)$_GET['pageSize'] : 10;
$offset = ($page - 1) * $pageSize;

$sql = "SELECT p.*, u.name, u.avatar 
        FROM posts p 
        LEFT JOIN users u ON p.user_id = u.id 
        WHERE p.status = 1 
        ORDER BY p.created_at DESC 
        LIMIT ?, ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $offset, $pageSize);
$stmt->execute();
$result = $stmt->get_result();

$posts = [];
while ($row = $result->fetch_assoc()) {
    // 格式化时间
    $row['created_at'] = date('Y-m-d H:i', strtotime($row['created_at']));
    $posts[] = $row;
}

echo json_encode([
    'status' => 'success',
    'posts' => $posts
]);

$stmt->close();
$conn->close();
?> 